package com.kool.leitu.service;

import com.kool.leitu.base.excel.ExcelReader;
import com.kool.leitu.base.excel.ExcelToSqlTransfer;
import com.kool.leitu.base.exception.AppException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;

/**
 * @description:
 * @author: tianhe
 * @create: 2019/02/01 17:18
 */
@Service
@Slf4j
public class SqlService {
    @Value("${templateDirPath}")
    private String templateDirPath;
    @Resource
    private ExcelReader excelReader;
    @Resource
    private ExcelToSqlTransfer excelToSqlTransfer;

    public String excelToSql(MultipartFile uploadFile) {
        String sql = null;

        try {
            InputStream is = uploadFile.getInputStream();
            //读取excel数据
            List<List<List<String>>> sheets = excelReader.read(uploadFile.getOriginalFilename(),is,1,65535);
            //excel数据转sql
            sql = excelToSqlTransfer.generate(sheets,templateDirPath,null);
        } catch (IOException e) {
            log.error("读输入流错误：",e);
            throw new AppException(2000001, e);
        }

        return sql;
    }
}
